package com.wjp.banquet.common.util;

import com.alibaba.fastjson.JSONObject;
import com.wjp.banquet.common.enums.ResponseEnum;
import com.wjp.banquet.common.vo.CommonRsp;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author 王君平
 * @Date 2023/9/13 11:23
 * @Description
 */
public class ResponseUtil {

    public static void writeErrorResponse(CommonRsp responseResult, HttpServletResponse response) {
        try {
            //把Result对象转换为JSON格式字符串
            response.setStatus(ResponseEnum.SUCCESS.getCode());//状态码设置一下
            String json = JSONObject.toJSONString(responseResult);
            response.setContentType("application/json;charset=utf-8");
            //将json字符串返回给页面
            if(response.isCommitted()){//TODO  临时方案  一定要找出为啥写了2遍流
                return;
            }
            response.getWriter().write(json);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
